from flask_restful import Resource

from restful.api__.public__.info.composite.post.list._PostsList import  _PostsList
from restful.api__.session import login_required_current_user_generate
from restful.model.query import QueryMultipleInstances, QueryOneInstance
from restful.model.tables import Check, Blog
from restful.response import ResponseSuccess

class PostsListByHot(Resource):
    @login_required_current_user_generate
    def get(self, current_user):
        # 获取所有通过审核的帖子
        checks = QueryMultipleInstances(Check, {'checked': True, 'passed': True})
        # 获取对应的 Blog 实例
        posts = [QueryOneInstance(Blog, {'id': i.pid}) for i in checks]
        posts_data = _PostsList(posts)
        sorted_posts = sorted(posts_data, key=lambda x: x['likes'], reverse=True)
        return ResponseSuccess(data=sorted_posts)
